const initMatch = (_self, type) => {
  var oldLocalVimData = _self.getter.VimOldData
  var localVimData = _self.getter.VimBaseData
  if (!localVimData) {
    return
  }
  var vimChanged = false
  var _type = parseInt(type)
  switch (_type) {
    case 1:
      vimChanged = _self.getter.VimChangedCcw
      _self.dispatch('setVimChangedCcw', false)
      break;
    case 2:
      vimChanged = _self.getter.VimChangedCth
      _self.dispatch('setVimChangedCth', false)
      break;
    case 3:
      vimChanged = _self.getter.VimChangedEscf
      _self.dispatch('setVimChangedEscf', false)
      break;
  }
  console.log(vimChanged)
  if (!vimChanged)
    return

  var oldResult = ''
  if (oldLocalVimData) {
    oldResult = oldLocalVimData.object.result.data
  }
  var result = localVimData.object.result.data
  // console.log(result)
  var needMatchData = []
  var index1 = 0, index2 = 0, index3 = 0, index4 = 0;
  for (var i = 0; i < result.decode.length; i ++) {
    var name = result.decode[i][0]
    if (name === '品牌') {
      index1 = i
    } else if (name === '车系') {
      index2 = i
    } else if (name === '车型' || name === '车辆配置') {
      index3 = i
    } else if (name === '出厂日期') {
      index4 = i
    }
  }
  needMatchData.push(result.decode[index1][1])
  needMatchData.push(result.decode[index2][1])
  needMatchData.push(result.decode[index4][1])
  needMatchData.push(result.decode[index3][1])

  var oldCarData = []
  if (oldResult) {
    var index_1 = 0, index_2 = 0, index_3 = 0, index_4 = 0;
    for (var i = 0; i < result.decode.length; i ++) {
      var name = result.decode[i][0]
      if (name === '品牌') {
        index_1 = i
      } else if (name === '车系') {
        index_2 = i
      } else if (name === '车型' || name === '车辆配置') {
        index_3 = i
      } else if (name === '出厂日期') {
        index_4 = i
      }
    }
    oldCarData.push(oldResult.decode[index1][1])
    oldCarData.push(oldResult.decode[index2][1])
    oldCarData.push(oldResult.decode[index4][1])
    oldCarData.push(oldResult.decode[index3][1])
  }
  if (judgeMatchStatus(_self, type, needMatchData)) {
    _self.dispatch('setCommonConditioToNull', '')
    _self.setCommonConditionToNull()
    _self.curSelect = ''
    _self.curChildSelect = ''
    _self.curSimleTime = ''
    _self.curModels = ''
    matchPpData(_self, needMatchData, type)
  }
  // if (!oldResult || needMatchData[0] !== oldCarData[0] || needMatchData[1] !== oldCarData[1] || needMatchData[2] !== oldCarData[2] || needMatchData[3] !== oldCarData[3]) {
  //   console.log('match')
  //   _self.curSelect = ''
  //   _self.curChildSelect = ''
  //   _self.curSimleTime = ''
  //   matchPpData(_self, needMatchData, type)
  // }
}

const judgeMatchStatus = (_self, type, needMatchData) => {
  var _type = parseInt(type)
  var condition = ''
  switch (_type) {
    case 1:
      condition = _self.getter.ccwCondition
      break
    case 2: 
      condition = _self.getter.cthCondition
      break
    case 3:
      condition = _self.getter.escfCondition
      break
  }
  if (!condition.curSelect) {
    // 需匹配 重置commonCondition
    return true
  } else if (condition.curSelect.pp_name === needMatchData[0]) {
      if (condition.curSelect.name == needMatchData[1] || condition.curSelect.name == (condition.curSelect.pp_name +  needMatchData[1]) 
          || condition.curSelect.name == (condition.curSelect.pp_name + ' ' + needMatchData[1])
          || ((condition.curSelect.pp_name + condition.curSelect.name) == (needMatchData[1]) || (condition.curSelect.pp_name + ' ' + condition.curSelect.name ) == (needMatchData[1]))) {
          if (condition.curSimleTime === needMatchData[2]) {
            return false
          }
      }
      return true
  } else {
    return true
  }
}

const matchPpData = (_self, needMatchData, type) => {
  var success = false
  if (_self.ppData) {
    var list = _self.ppData.list
    for (var i = 0; i < list.length; i ++) {
      var curItem = list[i]
      for (var k = 0; k < curItem.listByZimu.length; k ++) {
        var item = curItem.listByZimu[k]
        if (item.pp_name == needMatchData[0]) {
          _self.curSelect = item
          success = true
          break
        }
      }
      if (success) {
        break
      }
    }
  }
  // console.log(_self.curSelect)
  if (success) { // 匹配成功，继续匹配
    _self.getChildPP((res) => {
      // console.log(type)
      var curType = parseInt(type)
      switch (curType) {
        case _self.$const.CCWTYPE:
          matchCCWChildData(_self, res.list, needMatchData, type)
          break;
        case _self.$const.CTHTYPE:
          matchCTHChildData(_self, res, needMatchData, type)
          break;
        case _self.$const.ESCFTYPE:
          matchCTHChildData(_self, res, needMatchData, type)
          break;
      }
    })
  }
}
const matchCCWChildData = (_self, simpleData, needMatchData, type) => {
  // console.log('matchCCWChildData')
  // console.log(simpleData)
  var success = false
  for (var i = 0; i < simpleData.length; i ++) {
    var itemList = simpleData[i].itemList
    for (var k = 0; k < itemList.length; k ++) {
      var item = itemList[k]
      // console.log(item.name)
      if ((item.name == needMatchData[1] ||　item.name == (_self.curSelect.pp_name +  needMatchData[1]) || item.name == (_self.curSelect.pp_name + ' ' + needMatchData[1]))) {
        success = true
        _self.curChildSelect = item
        if ((needMatchData[2] >= item.launch_year && needMatchData[2] <= item.stop_year)) {
          _self.curSimleTime = needMatchData[2]
        }
        break
      } else if (((_self.curSelect.pp_name + item.name) == (needMatchData[1]) || (_self.curSelect.pp_name + ' ' + item.name) == (needMatchData[1]))) {
        success = true
        _self.curChildSelect = item
        if ((needMatchData[2] >= item.launch_year && needMatchData[2] <= item.stop_year)) {
          _self.curSimleTime = needMatchData[2]
        }
        break
      }
    }
    if (success) {
      break
    }
  }
}

const matchCTHChildData = (_self, simpleData, needMatchData, type) => {
  // // console.log('matchCTHChildData')
  // console.log(_self.curSelect)
  var success = false
  for (var i = 0; i < simpleData.length; i ++) {
    var itemList = simpleData[i].itemList
    for (var k = 0; k < itemList.length; k ++) {
      var item = itemList[k]
      if (item.name == needMatchData[1] ||　item.name == (_self.curSelect.pp_name +  needMatchData[1]) || item.name == (_self.curSelect.pp_name + ' ' + needMatchData[1])) {
        success = true
        _self.curChildSelect = item
        // console.log(item)
        break
      } else if (((_self.curSelect.pp_name + item.name) == (needMatchData[1]) || (_self.curSelect.pp_name + ' ' + item.name) == (needMatchData[1]))) {
        success = true
        _self.curChildSelect = item
        break
      }
    }
    if (success) {
      break
    }
  }
  if (success) {
    // console.log(_self.curChildSelect)
    _self.getChildPPSimple((res) => {
      matchCTHSimpleData(_self, res, needMatchData, type)
    })
  }
}

const matchCTHSimpleData = (_self, simpleData, needMatchData, type) => {
  // console.log('matchCTHSimpleData')
  var success = false
  // console.log(simpleData)
  for (var i = 0; i < simpleData.timeLsit.length; i ++) {
    var time = simpleData.timeLsit[i]
    if (time == needMatchData[2]) {
      _self.curSimleTime = time
    }
  }
}

export default {
  initMatch
}